# coding:utf-8
import base64
import time
import threading

import hashlib


# def run(name):
#     print(name)
#     time.sleep(2)
#
#
# threads = [threading.Thread(target=run, args=(i,)) for i in range(10)]  # 用列表生成式 生成10个线程
# for t in threads:  # 启动刚刚创建的10个线程
#     t.start()


def md_5(word):
    if isinstance(word, bytes):
        b_str = word
    else:
        b_str = str.encode(word)
    m = hashlib.md5()
    m.update(b_str)
    md5_str = m.hexdigest()
    return md5_str


def base_64(word):
    if isinstance(word, bytes):
        b_str = word
    else:
        b_str = str.encode(word)
    m = base64.b64encode(b_str).decode()
    return m


def content_md5(data):
    """计算data的MD5值，经过Base64编码并返回str类型。

    返回值可以直接作为HTTP Content-Type头部的值
    """
    if isinstance(data, str):
        data = hashlib.md5(data.encode('utf-8'))
    value = base64.b64encode(data.hexdigest().encode('utf-8'))
    return value.decode('utf-8')


if __name__ == '__main__':
    words = '123456'
    secret = 'foih91etr0g4gp1i'
    seed = md_5(words)
    sign = md_5(words + secret)

    auths = "admin:WWW.xxzx@789"
    auth = "b2fpip4bpe:8fe74289-0ecb-4f84-aae2-e389aa92f86d"
    auth_str = base_64(auth)
    print(base_64(auths))
    # Authorize=Basic%20YjJmcGlwNGJwZTo4ZmU3NDI4OS0wZWNiLTRmODQtYWFlMi1lMzg5YWE5MmY4NmQ=
    #           Basic%20YjJmcGlwNGJwZTo4ZmU3NDI4OS0wZWNiLTRmODQtYWFlMi1lMzg5YWE5MmY4NmQ=
    headers = {"content-type": "application/x-www-form-urlencoded",
               "Authorize": "Basic%20{}".format(auth_str)}
    print(seed, sign)
    print(headers)
    headers = {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-SMS-SEED': '{}'.format(words),
        'X-SMS-SIGN': '{}'.format(sign),
        'Authorization': '{}'.format(auth_str),
    }
    print(headers)
